
import javax.swing.*;
import java.util.ArrayList;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

public class Scores 
{

	Object[] columns = {"Name", "Level", "Score", "Difficulty"};
	Object[][] scores = new String[10][4];
	String filename = "Scores.txt";
	private int score;
	private int level;
	
	String[][] table = new String[10][5];
	
	JFrame content;
	
	public Scores()
	{
		generateDefaults();
		if(!this.readFile(filename))
		{
			generateDefaults();
		}
		score = 0;
		level = 0;
		
		content = new JFrame();
		content.setSize(900, 300);
		content.setResizable(false);
		content.setVisible(false);
		
		JTable data = new JTable(scores, columns);
		data.getTableHeader();
		content.add(data);
		
	}
	
	/**
	 * displays the scores from previous games
	 */
	public void displayScores()
	{
		content.setVisible(true);
	}
	
	/**
	 * adds score that player just recieved into
	 * the list of previous scores.
	 */
	public static void addNewScore(int score, int level)
	{
		// compare new score versus old score.
		// if new score is bigger than an old one then replace
		
		
	}
	
	/**
	 * reads the scores and names from a file
	 * that holds them while the game is not
	 * being played.
	 * @return
	 */
	private boolean readFile(String filename)
	{
		boolean error = false;
		String temp;
		
		try
		{
			FileReader infile = new FileReader(filename);
			BufferedReader buff = new BufferedReader(infile);
			
			temp = buff.readLine();
			while(temp != null)
			{
				temp = buff.readLine();
			}
		}
		catch(FileNotFoundException e)
		{
			System.out.println("readFile: FileNotFoundException has occurred.");
			error = true;
		}
		catch(IOException e)
		{
			System.out.println("readFile: IOException has occurred.");
			error = true;
		}
		
		return error;
	}
	
	private boolean writeFile(String fileName)
	{
		boolean error = true;
		
		File inFile = new File(filename);
		FileWriter fwriter;
		BufferedWriter buff;
		
		try
		{
			fwriter = new FileWriter(inFile);
			buff = new BufferedWriter(fwriter);
			
			buff.close();
			
			error = false;
				
		}
		catch(IOException e)
		{
			System.out.println("IOException has occurred.");
			error = true;
		}
		
		return error;
	}
	
	
	/**
	 *  
	 */
	private boolean updateScores()
	{
		boolean error = false;
		
		
		
		return error;
	}
	
	private void generateDefaults()
	{
		table[0][0] = "Jordan";
		table[0][1] = "17";
		table[0][2] = "234";
		table[0][3] = "2";
		
		table[1][0] = "Reed";
		table[1][1] = "16";
		table[1][2] = "233";
		table[1][3] = "2";

		table[2][0] = "Bob";
		table[2][1] = "4";
		table[2][2] = "71";
		table[2][3] = "2";
		
		table[3][0] = "Jamie";
		table[3][1] = "";
		table[3][2] = "";
		table[3][3] = "";

		table[4][0] = "Thompson";
		table[4][1] = "";
		table[4][2] = "";
		table[4][3] = "";

		table[5][0] = "Lucil";
		table[5][1] = "";
		table[5][2] = "";
		table[5][3] = "";

		table[6][0] = "Jennifer";
		table[6][1] = "";
		table[6][2] = "";
		table[6][3] = "";

		table[7][0] = "Andy";
		table[7][1] = "";
		table[7][2] = "";
		table[7][3] = "";

		table[8][0] = "Jermaine";
		table[8][1] = "";
		table[8][2] = "";
		table[8][3] = "";

		table[9][0] = "Tiffany";
		table[9][1] = "";
		table[9][2] = "";
		table[9][3] = "";
	}
	
}
